      1  /**

      2     A program that demonstrates the LinkedList class

      3  */

      4  public class ListDemo

      5  {

      6     public static void main(String[] args)

      7     {

      8        LinkedList staff = new LinkedList();

      9        staff.addLast("Diana");

     10        staff.addLast("Harry");

     11        staff.addLast("Romeo");

     12        staff.addLast("Tom");

     13

     14        // | in the comments indicates the iterator position

     15

     16        ListIterator iterator = staff.listIterator(); // |DHRT

     17        iterator.next(); // D|HRT

     18        iterator.next(); // DH|RT

     19

     20        // Add more elements after second element

     21

     22        iterator.add("Juliet"); // DHJ|RT

     23        iterator.add("Nina"); // DHJN|RT

     24

     25        System.out.println(iterator.next()); // DHJNR|T

     26        System.out.println(iterator.previous()); // DHJN|RT

     27

     28        // Remove last traversed element

     29

     30        iterator.remove(); // DHJN|T

     31

     32        // Print all elements

     33

     34        iterator = staff.listIterator();

     35        while (iterator.hasNext())

     36        {

     37           System.out.print(iterator.next() + " ");

     38        }

     39        System.out.println();

     40     }

     41  }